package servlet;

import dao.UserDao;
import dao.UserDaoImpl;
import pojo.User;
import service.UserService;
import service.UserServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/register",initParams = {
        @WebInitParam(name="encoding",value = "utf-8")})
public class RegisterServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    String encoding = null;
    @Override
    public void init(ServletConfig config) {
         encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        User user = new User();
        user.setPassword(req.getParameter("password"));
        user.setUsername(req.getParameter("username"));
        int i = userService.addUser(user);
        if (i!=0){
            req.setAttribute("msg","注册成功");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }
    }
}
